local juguan = fk.CreateSkill{
    name = "wk_heg__juguan",
}

Fk:loadTranslationTable{
    ["wk_heg__juguan"] = "拒关",
    [":wk_heg__juguan"] = "出牌阶段限一次，你可以令任意名你攻击范围内的角色依次视为对你使用一张【决斗】，然后你摸X张牌并视为对其中未以此法对你造成伤害的角色使用一张【杀】（X为其中以此法对你造成伤害的角色数）。",
    ["#wk_heg__juguan_choose"] = "拒关：选择你攻击范围内任意名角色，这些角色依次视为对你使用一张【决斗】",
    ["#wk_heg__juguan"] = "拒关：你可以令任意名你攻击范围内的角色依次视为对你使用一张【决斗】",

    ["$wk_heg__juguan1"] = "吾欲自立，举兵拒关。",
    ["$wk_heg__juguan2"] = "自立门户，拒关不开。",
}

juguan:addEffect("active",{
  anim_type = "control",
  card_num = 0,
  target_num = 0,
  prompt = "#wk_heg__juguan",
  can_use = function(self, player)
    return player:usedSkillTimes(juguan.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local targets = table.filter(room.alive_players, function(p) return player:inMyAttackRange(p) end)
    if #targets > 0 then
      local tos = room:askToChoosePlayers(player,{
        targets = targets,
        min_num = 1,
        max_num = #targets,
        prompt = "#wk_heg__juguan_choose",
        skill_name = juguan.name,
        cancelable = true,
      })
      room:sortByAction(tos)
      for i = 1, #tos, 1 do
        local from = tos[i]
        room:useVirtualCard("duel", nil, from, player, juguan.name, true)
        if player.dead then return end
      end
      local slash_targets = {}
      local draw_num = 0
      if not player.dead then
        for i = 1, #tos, 1 do
          local from = tos[i]
          if from:getMark("wk_heg__juguan_record-phase") == 1 then
            draw_num = draw_num + 1
          elseif not from.dead then
            table.insert(slash_targets, from)
          end
        end
        player:drawCards(draw_num, juguan.name)
        if #slash_targets > 0 and not player.dead then
          room:useVirtualCard("slash", nil, player, slash_targets, juguan.name, true)
        end
      end
    end
  end,
})

juguan:addEffect(fk.Damage,{
  is_delay_effect = true,
  anim_type = "control",
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(juguan.name, true) and data.card and data.card.trueName == "duel" and not data.from.dead and table.contains(data.card.skillNames, juguan.name)
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(data.from, "wk_heg__juguan_record-phase", 1)
  end,
})

return juguan